package com.stary.springmvc.web.json;

import com.stary.springmvc.web.json.entity.Dog;
import com.stary.springmvc.web.json.entity.User;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Guan
 **/
// @Controller
//@ResponseBody写在类上，相当于类中的所有方法都被该注解修饰（根据需求使用）
//如果前端传来json格式的数据后端没有进行处理，会报错404
// @ResponseBody
//@Controller + @ResponseBody = @RestController
@RestController
public class JsonHandler {
    /**
     * 1.被@ResponseBody注解修饰，返回的结果是json格式
     * 2.根据http请求进行处理（如何理解？）
     * 3.使用原生的springmvc转换器——HttpMessageConvert
     * 4.返回的不止可以对象，也可以是集合 public List<Dog> JsonHandler()
     *  @return
     */
    @RequestMapping("json/dog")
    // @ResponseBody
    public Dog DogHandler() {
        Dog dog = new Dog();
        dog.setName("dr杰");
        dog.setAddress("关家");
        return dog;
    }

    @RequestMapping("json/dogs")
    // @ResponseBody
    public List<Dog> DogsHandler() {
        List<Dog> pets = new ArrayList<>();
        Dog dog = new Dog();
        dog.setName("dr杰");
        dog.setAddress("关家");

        Dog dog2 = new Dog();
        dog2.setName("dr杰1");
        dog2.setAddress("关家1");

        pets.add(dog);
        pets.add(dog2);
        return pets;
    }





    /**
     * 1.@RequestBody User user在形参制定了@RequestBody注解
     * 2.springmvc就会交提交的json字符串数据转换成指定的javabean
     * 3.注意：前后端的属性值应该匹配，否则会返回空
     * 4.如果没有@RequestBody修饰，该属性的前后端的值都为空。
     * @param user
     * @return
     */

    @ResponseBody
    @RequestMapping("json/user")
    public User UserHandler(@RequestBody User user) {
        //接收前台传入的json数据，并以json的格式传回浏览器
        System.out.println("user = " + user);
        return user;
    }

    @RequestMapping("download")
    public ResponseEntity<byte[]> download(HttpSession httpSession) throws IOException {
        //1.先获取下载文件的inputStream
        InputStream resourceAsStream = httpSession.getServletContext().getResourceAsStream("/img/1.jpg");
        InputStream resourceAsStream2 =
                httpSession.getServletContext().getResourceAsStream("/img/1.jpg");
        //2.按获取文件的长度设置一个byte数组
        byte[] bytes = new byte[resourceAsStream.available()];
        //将resourceAsStream（文件数据）读入数组中
        resourceAsStream.read(bytes);
        //3.查看ResponseEntity的构造函数，并进行填充
        //ResponseEntity的构造函数：public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatus status)
        //解读：
        //  1，body为具体内容，即bytes，下载的文件数据
        //  2. MultiValueMap<String, String> headers为请求头信息，HttpHeaders实现了MultiValueMap接口（http响应头headers）
        //  3. status为 http的响应状态
        HttpHeaders httpHeaders = new HttpHeaders();
        //content-type 指示响应内容的格式
        //content-disposition指示如何处理响应内容
        //指定返回的数据，客户端当以附件形式处理
        httpHeaders.add("Content-Disposition","attachment;filename=1.jpg");
        HttpStatus ok = HttpStatus.OK;
        //构建ResponseEntity对象
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, httpHeaders, ok);
        return responseEntity;
    }


}
